﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FindAllArea1.aspx.cs" Inherits="HZ.Supervise.Web.Common.FindAllArea1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>选择地区</title>
    <link rel="stylesheet" href="../lib/demoStyle/demo.css" type="text/css" />
    <link rel="stylesheet" href="../lib/zStyle/zStyle.css" type="text/css" />
    <link rel="Stylesheet" href="../lib/ZheZao/defilade_other.css" />
    <link rel="stylesheet" type="text/css" href="../style/global.css" />

    <script type="text/javascript" src="../Scripts/jquery-1.4.2.js"></script>

    <script type="text/javascript" src="../Scripts/jquery_1.js"></script>

    <script type="text/javascript" src="../lib/ZheZao/defilade_other.js"></script>

    <script type="text/javascript" src="../scripts/mainlistSelect.js"></script>

    <script type="text/javascript" src="../scripts/Common.js"></script>

    <script type="text/javascript" src="../scripts/global.js"></script>

    <script type="text/javascript" language="javascript">

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //树形菜单变量
        var ztree;

        //树形菜单设置变量
        var setting;

        //存储选取的ID
        var GetDataByid = "";

        //存储选取的名称
        var GetDataByName = "";

        //设置树形菜单
        setting = {
            checkable: true,
            async: true,
            isParent: true,
            asyncUrl: "../CommonProgram/ProcessTree.ashx?areid= " + "<%=GetAreaId()%>"
        };

        //jquery ajax 生成树形菜单
        //董辉 2011-4-24 23:34
        $(document).ready(function() {
            showBg("dialog");

            ztree = $("#treeDemo").zTree(setting);
        });

        //获取取的树形菜单节点数据
        //董辉 2011-4-24 23:37
        function getdata() {
            var temp = ztree.getChangeCheckedNodes();



            GetDataByid = "";

            GetDataByName = "";

            var list = document.getElementById("ListArea");

            var option = document.createElement("OPTION");

            list.length = 0;

            //add by 曹鹏 2011年4月27日19:53:03 实现功能（当父节点下的子节点都选中时，直接将父节点加入到比对项，子节点不参与比对。当父节点下的子节点没全选中，将子节点加入比对中，父节点不参与比对）

            var count = 0; //计数参数

            $.each(temp, function(key, value) {


                if (value.level == 0 && value.isParent) {//当是顶级节点，并且判断是否是父节点

                    var sunArray = value.nodes; //取得该节点下面所有的子节点

                    count = 0; //计数参数初始化为0

                    $.each(sunArray, function(key1, value1) {//遍历，计算该节点子节点选中的数量

                        if (value1.checked) {

                            count++;

                        }

                    });

                    if (sunArray.length == count) {//如果该节点子节点选中的数量等于该节点拥有子节点的数量，那么直接把该节点加到listarea中区

                        var listItem = document.createElement("OPTION"); //动态创建一个option标签
                        listItem.text = value.name;
                        listItem.value = value.id;
                        GetDataByid += value.id + ",";
                        GetDataByName += value.name + ",";
                        list.add(listItem); //将标签加入到listarea中

                    }
                    else {//否则的话，将该节点下面的所有子节点加进去

                        $.each(sunArray, function(key2, value2) {

                            if (value2.checked) {

                                var listItem = document.createElement("OPTION");
                                listItem.text = value2.name;
                                listItem.value = value2.id;
                                GetDataByid += value2.id + ",";
                                GetDataByName += value2.name + ",";
                                list.add(listItem);

                            }
                        });
                    }
                }
                else if (value.level == 0 && (!value.isParent)) {//顶级节点，但其下面没有子节点

                    if (value.checked) {
                        var listItem = document.createElement("OPTION");
                        listItem.text = value.name;
                        listItem.value = value.id;
                        GetDataByid += value.id + ",";
                        GetDataByName += value.name + ",";
                        list.add(listItem);
                    }
                }

            });

            document.getElementById("AreaName").value = GetDataByName;

            document.getElementById("AreaId").value = GetDataByid;

            if (list.length > 8) {//当比对项超过8条(给出建议但仍然可以比对，需求变更再改)

                //                while (list.length > 0) {

                //                    list.remove(list[0]);

                //                } //去掉listArea所有项

                alert("建议要对比的地区数量不要超过8个！");


            }
            else if (list.length == 0) {//当用户没有选中任何一条

                alert("请选择要对比的地区！");

            }



            //            $.each(temp, function(key, value) {
            //                if (temp == null) {
            //                    alert("请选择要对比的地区！");
            //                }
            //                else if (key < 8) {
            //                    GetDataByid += value.id + ",";

            //                    GetDataByName += value.name + ",";
            //                }

            //                count++;
            //            })

            //            if (count > 8) {
            //                alert("建议要对比的地区数量不要超过8个！");
            //            }
            //            else if (count == 0) {
            //                alert("请选择要对比的地区！");
            //            }
            //            else {

            //                $.each(temp, function(key, value) {
            //                    var listItem = document.createElement("OPTION");

            //                    listItem.text = value.name;

            //                    list.add(listItem);
            //                })

            //                            document.getElementById("AreaName").value = GetDataByName;

            //                            document.getElementById("AreaId").value = GetDataByid;
            //            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



        function addAreaInfo() {
            var AreaInfo;

            if (GetDataByid == "" || GetDataByName == "") {
                alert("请选择要对比的地区！");

                return false;
            }

            AreaInfo = document.getElementById("AreaName").value + "|" + document.getElementById("AreaId").value;
            window.opener.insertAreaInfo(AreaInfo);
            window.close();
        }

        function chkAllCompany() {
            CheckBoxList = document.getElementsByName("chkArea");
            for (var i = 0; i < CheckBoxList.length; i++) {
                CheckBoxList[i].checked = true;
            }
        }

        function deleteCompany() {
            if (!isNull(checkSelected())) {
                var info = checkSelected();
                alert(info);
                return;
            }
            var listbox = document.getElementById("ListArea");
            var companyID = document.getElementById("AreaId");
            var companyName = document.getElementById("AreaName");
            companyID.value = "";
            companyName.value = "";

            var selectindex = listbox.selectedIndex;
            listbox.remove(selectindex);

            for (j = listbox.options.length - 1; j >= 0; j--) {
                companyID.value = companyID.value + listbox[j].value + ",";
                companyName.value = companyName.value + listbox[j].text + ",";
            }
        }

        function checkSelected() {
            var info;
            var listbox = document.getElementById("ListArea");
            var selectindex = listbox.selectedIndex;
            if (selectindex < 0) {
                info = "请选择要删除的地区！";
                return info;
            }
            return "";
        }

        function isNull(str) {
            var re = /^[ \s]*$/;
            return re.test(str);
        }
    </script>

    <style type="text/css">
        .style1
        {
            width: 126px;
        }
        .style2
        {
            width: 333px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="querylist">
        <tr>
            <td class="style2">
                选择地区：
            </td>
            <td class="style1">
                &nbsp;
            </td>
            <td width="40%">
                对比地区：
            </td>
        </tr>
        <tr>
            <td class="style2">
                <div style="padding-left: 100px">
                    <font color="red">提示：建议要对比的地区数量不要超过8个！</font></div>
                <div id="show" class="zTreeDemoBackground">
                    <ul id="treeDemo" class="tree">
                    </ul>
                </div>
            </td>
            <td align="left" class="style1">
                <input type="button" id="btnIn" value="加入对比" class="btn" onclick="getdata();" />
                <br />
                <br />
                <input type="button" id="btnOut" value="删除对比" class="btn" onclick="deleteCompany();" />
                <br />
                <br />
                <input type="button" id="btnOk" value="确认对比" class="btn" onclick="return addAreaInfo();" />
            </td>
            <td rowspan="2" style="padding-top: 0px;">
                <input id="AreaId" type="hidden" name="HosId" runat="server" />
                <input id="AreaName" type="hidden" name="HosName" runat="server" />
                <asp:ListBox ID="ListArea" runat="server" Height="450px" Width="300px"></asp:ListBox>
            </td>
        </tr>
        <%--        <tr>
            <td class="style2">
                &nbsp;
            </td>
            <td class="style1">
            </td>
        </tr>--%>
    </table>
    <div id="fullbg">
    </div>
    <img id="dialog" src="../lib/images/loading.gif" alt="" />
    </form>
</body>
</html>
